Plotly Package Review

Jonathan Yu, Jaden Stanford, Joseph Keogh

Package Overview

History of Plotly

Plotly is a company founded by Alex Johnson, Jack Parmer, Chris Parmer, and Matthew Sundquistin 2013. While working for the data science program of a California-based cleantech company, Alex, Jack, Chris, and Matthew found themselves facing the seemingly simple problem of having to find a way to meaningfully and easily share the data they’ve gathered. Even after collecting, analyzing, and sorting data, they felt that there were still important questions that had to be answered. These questions included:

  • How do we share what we’ve learned with others in a meaningful way?
  • How do we enable others to explore our data?
  • Can we give others access to the models to explore on their own?

To answer these questions they decided to create a tool to make scientific and data analysis simple. Plotly was actually originally a JavaScript graphing library which was eventually converted into an R package which allowed the creation of graphs using R data and syntax from ggplot2. Their focuses with plotly were to use the web as a data science platform, power discovery with open source, provide unlimited flexibility, remove language as a barrier, and enable shared goals across the organization. In 2013 and 2014, Jack, Chris, Matthew, and Alex officially founded “plotly” and opened their Montreal headquarters.

Background of Plotly

Plotly is a graphing library that makes interactive, publication-quality graphs. Oftentimes with data visualization, people run into issues with making graphs both interesting and informative. However, with plotly this can be done easily as plotly allows for the creation of a wide variety of visualizations ranging from basic bar-graphs to maps, 3d charts, and animated charts with a few lines of code. In addition to being easy to use, plotly is also an open-source package that can be found on numerous platforms including R, Python, .NET, JS, and Julia.

Version History

Current Version: 4.9.3 (last updated 1/10/2021)

Dependencies

R (≥ 3.2.0), ggplot2 (≥ 3.0.0)

Usage

plotly offers a wide variety of interactive options allowed on a web-based application which include:

Examples of Usage

plotly converts plots to an interactive, web-based version. It allows for zooming in or out of a graph, selection of data points

  • zooming in or out of a graph
  • selection of data points upon mouse hover
  • panning through a graph
  • downloading a graph as a PNG
  • box selecting or lasso selecting data points
  • option to compare nearby data points upon hover

visual formats supported by plotly include:

  • basic charts (scatter and line plots, bar charts, pie charts, bubble charts, and more)
  • statistical charts (2D Histograms, box plots, histograms, error bars, violin plots, and more)
  • scientific charts (log plots, contour plots, heatmaps, network graph, ternary contour plots, and more)
  • financial charts (time series, candlestick charts, OHLC charts, waterfall charts, funnel charts, and more)
  • maps (chloropleth maps, scatter plots on maps, mapbox density, lines on maps, mapbox layers, and more)
  • 3D Charts (3D scatter plots, 3d line plots, 3d surface plots, 3d mesh plots, 3d cone plots, and more)
  • subplots (multiple axes, map subplots and small multiples, inset plots, subplots, 3d subplots, and more)
  • animated plots

Bubble Chart

One of the most basic charts that you are able to create in plotly is the bubble chart. A bubble chart is a scatter plot whose markers have variable color and size. In this example we will be making a bubble plot from scratch using plotly and the built in mtcars dataset in R.

#plot_ly function allows for creation of any plotly graph
figure <- plot_ly(
  data = mtcars, #specified dataset, in this case we are using mtcars
  x = ~mpg, #variable that corresponds with x-axis 
  y = ~hp, #variable that corresponds with y-axis
  text = rownames(mtcars), #specifies text that shows up when you hover over a data point
  type = 'scatter', #specified graph type, in this case we are using the scatter plot
  mode = 'markers', 
  marker = list(size = ~cyl, opacity = 0.5, color = 'green')) #marker parameter specifies the size, opacity, and color of each of our bubbles.  


figure <- figure %>% #plotly uses the pipe function from dplyr for formatting the layout of the graph (x-axis, y-axis, and title)
  layout(title = 'Gas Efficiency and Speed of Cars in mtcars dataset', 
         xaxis = list(title = 'Miles Per Gallon'),
         yaxis = list(title = 'Horsepower'))
figure
<<<<<<< HEAD
======= <<<<<<< HEAD
======= <<<<<<< HEAD
=======
>>>>>>> 595fff5556ffc5041ce8006955a6476aee16dd06 >>>>>>> cb3f12a9bb56803e7f87a128236da8efc0477c80 >>>>>>> b623b5b9a05678bddd9946a9764ade851a3847d7

You can also add more to what can be shown in a plotly graph by adding the ‘text’ parameter ‘hoverinfo’.

figure <- plot_ly(
  data = mtcars,
  x = ~mpg,
  y = ~hp,
  hoverinfo = 'text', #hoverinfo parameter added
  text = ~paste('Car Model:', rownames(mtcars), '<br>Displacement:', disp), #text parameter changed
  type = 'scatter', 
  mode = 'markers', 
  marker = list(size = ~cyl, opacity = 0.5, color = 'green')) 


figure <- figure %>%
  layout(title = 'Gas Efficiency and Speed of Cars in mtcars dataset', 
         xaxis = list(title = 'Miles Per Gallon'),
         yaxis = list(title = 'Horsepower'))
figure
<<<<<<< HEAD
======= <<<<<<< HEAD
======= <<<<<<< HEAD
>>>>>>> b623b5b9a05678bddd9946a9764ade851a3847d7

Heatmaps

Heatmaps are graphs that use a color scale to illustrate relationships between variables. In order to easily create a heatmap in plotly, you have to first convert your dataset into a matrix. This can be done with the as.matrix function in R. After that, it is important to normalize the different variables since the heatmap uses the same color scale for all variables so some variables with lower/higher numbers will not show up on the color scale. In this example, we will be using the USArrests dataset built into R.

USArrests <- USArrests[,-c(4)]
usarrests_matrix <- as.matrix(USArrests) #turning mtcars dataset into a matrix
usarrests_matrix <- apply(USArrests, 2, function(x) {x/mean(x)}) #normalizing the variables within mtcars_matrix
plot <- plot_ly (x = colnames(usarrests_matrix), #specifies variables on x-axis
                 y = rownames(usarrests_matrix), #specifies variables on y-axis
                 z = usarrests_matrix, #specifies data being used
                 type = "heatmap") #specifies type of graph 

plot
<<<<<<< HEAD
=======
=======
>>>>>>> cb3f12a9bb56803e7f87a128236da8efc0477c80

Heatmaps

Heatmaps are graphs that use a color scale to illustrate relationships between variables. In order to create a heatmap in plotly, you have to first convert your dataset into a matrix. This can be done with the as.matrix function in R. In this example, we will be using the Iris dataset to create a plotly heatmap from scratch.

iris_matrix <- as.matrix(iris)
iris
##     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
## 1            5.1         3.5          1.4         0.2     setosa
## 2            4.9         3.0          1.4         0.2     setosa
## 3            4.7         3.2          1.3         0.2     setosa
## 4            4.6         3.1          1.5         0.2     setosa
## 5            5.0         3.6          1.4         0.2     setosa
## 6            5.4         3.9          1.7         0.4     setosa
## 7            4.6         3.4          1.4         0.3     setosa
## 8            5.0         3.4          1.5         0.2     setosa
## 9            4.4         2.9          1.4         0.2     setosa
## 10           4.9         3.1          1.5         0.1     setosa
## 11           5.4         3.7          1.5         0.2     setosa
## 12           4.8         3.4          1.6         0.2     setosa
## 13           4.8         3.0          1.4         0.1     setosa
## 14           4.3         3.0          1.1         0.1     setosa
## 15           5.8         4.0          1.2         0.2     setosa
## 16           5.7         4.4          1.5         0.4     setosa
## 17           5.4         3.9          1.3         0.4     setosa
## 18           5.1         3.5          1.4         0.3     setosa
## 19           5.7         3.8          1.7         0.3     setosa
## 20           5.1         3.8          1.5         0.3     setosa
## 21           5.4         3.4          1.7         0.2     setosa
## 22           5.1         3.7          1.5         0.4     setosa
## 23           4.6         3.6          1.0         0.2     setosa
## 24           5.1         3.3          1.7         0.5     setosa
## 25           4.8         3.4          1.9         0.2     setosa
## 26           5.0         3.0          1.6         0.2     setosa
## 27           5.0         3.4          1.6         0.4     setosa
## 28           5.2         3.5          1.5         0.2     setosa
## 29           5.2         3.4          1.4         0.2     setosa
## 30           4.7         3.2          1.6         0.2     setosa
## 31           4.8         3.1          1.6         0.2     setosa
## 32           5.4         3.4          1.5         0.4     setosa
## 33           5.2         4.1          1.5         0.1     setosa
## 34           5.5         4.2          1.4         0.2     setosa
## 35           4.9         3.1          1.5         0.2     setosa
## 36           5.0         3.2          1.2         0.2     setosa
## 37           5.5         3.5          1.3         0.2     setosa
## 38           4.9         3.6          1.4         0.1     setosa
## 39           4.4         3.0          1.3         0.2     setosa
## 40           5.1         3.4          1.5         0.2     setosa
## 41           5.0         3.5          1.3         0.3     setosa
## 42           4.5         2.3          1.3         0.3     setosa
## 43           4.4         3.2          1.3         0.2     setosa
## 44           5.0         3.5          1.6         0.6     setosa
## 45           5.1         3.8          1.9         0.4     setosa
## 46           4.8         3.0          1.4         0.3     setosa
## 47           5.1         3.8          1.6         0.2     setosa
## 48           4.6         3.2          1.4         0.2     setosa
## 49           5.3         3.7          1.5         0.2     setosa
## 50           5.0         3.3          1.4         0.2     setosa
## 51           7.0         3.2          4.7         1.4 versicolor
## 52           6.4         3.2          4.5         1.5 versicolor
## 53           6.9         3.1          4.9         1.5 versicolor
## 54           5.5         2.3          4.0         1.3 versicolor
## 55           6.5         2.8          4.6         1.5 versicolor
## 56           5.7         2.8          4.5         1.3 versicolor
## 57           6.3         3.3          4.7         1.6 versicolor
## 58           4.9         2.4          3.3         1.0 versicolor
## 59           6.6         2.9          4.6         1.3 versicolor
## 60           5.2         2.7          3.9         1.4 versicolor
## 61           5.0         2.0          3.5         1.0 versicolor
## 62           5.9         3.0          4.2         1.5 versicolor
## 63           6.0         2.2          4.0         1.0 versicolor
## 64           6.1         2.9          4.7         1.4 versicolor
## 65           5.6         2.9          3.6         1.3 versicolor
## 66           6.7         3.1          4.4         1.4 versicolor
## 67           5.6         3.0          4.5         1.5 versicolor
## 68           5.8         2.7          4.1         1.0 versicolor
## 69           6.2         2.2          4.5         1.5 versicolor
## 70           5.6         2.5          3.9         1.1 versicolor
## 71           5.9         3.2          4.8         1.8 versicolor
## 72           6.1         2.8          4.0         1.3 versicolor
## 73           6.3         2.5          4.9         1.5 versicolor
## 74           6.1         2.8          4.7         1.2 versicolor
## 75           6.4         2.9          4.3         1.3 versicolor
## 76           6.6         3.0          4.4         1.4 versicolor
## 77           6.8         2.8          4.8         1.4 versicolor
## 78           6.7         3.0          5.0         1.7 versicolor
## 79           6.0         2.9          4.5         1.5 versicolor
## 80           5.7         2.6          3.5         1.0 versicolor
## 81           5.5         2.4          3.8         1.1 versicolor
## 82           5.5         2.4          3.7         1.0 versicolor
## 83           5.8         2.7          3.9         1.2 versicolor
## 84           6.0         2.7          5.1         1.6 versicolor
## 85           5.4         3.0          4.5         1.5 versicolor
## 86           6.0         3.4          4.5         1.6 versicolor
## 87           6.7         3.1          4.7         1.5 versicolor
## 88           6.3         2.3          4.4         1.3 versicolor
## 89           5.6         3.0          4.1         1.3 versicolor
## 90           5.5         2.5          4.0         1.3 versicolor
## 91           5.5         2.6          4.4         1.2 versicolor
## 92           6.1         3.0          4.6         1.4 versicolor
## 93           5.8         2.6          4.0         1.2 versicolor
## 94           5.0         2.3          3.3         1.0 versicolor
## 95           5.6         2.7          4.2         1.3 versicolor
## 96           5.7         3.0          4.2         1.2 versicolor
## 97           5.7         2.9          4.2         1.3 versicolor
## 98           6.2         2.9          4.3         1.3 versicolor
## 99           5.1         2.5          3.0         1.1 versicolor
## 100          5.7         2.8          4.1         1.3 versicolor
## 101          6.3         3.3          6.0         2.5  virginica
## 102          5.8         2.7          5.1         1.9  virginica
## 103          7.1         3.0          5.9         2.1  virginica
## 104          6.3         2.9          5.6         1.8  virginica
## 105          6.5         3.0          5.8         2.2  virginica
## 106          7.6         3.0          6.6         2.1  virginica
## 107          4.9         2.5          4.5         1.7  virginica
## 108          7.3         2.9          6.3         1.8  virginica
## 109          6.7         2.5          5.8         1.8  virginica
## 110          7.2         3.6          6.1         2.5  virginica
## 111          6.5         3.2          5.1         2.0  virginica
## 112          6.4         2.7          5.3         1.9  virginica
## 113          6.8         3.0          5.5         2.1  virginica
## 114          5.7         2.5          5.0         2.0  virginica
## 115          5.8         2.8          5.1         2.4  virginica
## 116          6.4         3.2          5.3         2.3  virginica
## 117          6.5         3.0          5.5         1.8  virginica
## 118          7.7         3.8          6.7         2.2  virginica
## 119          7.7         2.6          6.9         2.3  virginica
## 120          6.0         2.2          5.0         1.5  virginica
## 121          6.9         3.2          5.7         2.3  virginica
## 122          5.6         2.8          4.9         2.0  virginica
## 123          7.7         2.8          6.7         2.0  virginica
## 124          6.3         2.7          4.9         1.8  virginica
## 125          6.7         3.3          5.7         2.1  virginica
## 126          7.2         3.2          6.0         1.8  virginica
## 127          6.2         2.8          4.8         1.8  virginica
## 128          6.1         3.0          4.9         1.8  virginica
## 129          6.4         2.8          5.6         2.1  virginica
## 130          7.2         3.0          5.8         1.6  virginica
## 131          7.4         2.8          6.1         1.9  virginica
## 132          7.9         3.8          6.4         2.0  virginica
## 133          6.4         2.8          5.6         2.2  virginica
## 134          6.3         2.8          5.1         1.5  virginica
## 135          6.1         2.6          5.6         1.4  virginica
## 136          7.7         3.0          6.1         2.3  virginica
## 137          6.3         3.4          5.6         2.4  virginica
## 138          6.4         3.1          5.5         1.8  virginica
## 139          6.0         3.0          4.8         1.8  virginica
## 140          6.9         3.1          5.4         2.1  virginica
## 141          6.7         3.1          5.6         2.4  virginica
## 142          6.9         3.1          5.1         2.3  virginica
## 143          5.8         2.7          5.1         1.9  virginica
## 144          6.8         3.2          5.9         2.3  virginica
## 145          6.7         3.3          5.7         2.5  virginica
## 146          6.7         3.0          5.2         2.3  virginica
## 147          6.3         2.5          5.0         1.9  virginica
## 148          6.5         3.0          5.2         2.0  virginica
## 149          6.2         3.4          5.4         2.3  virginica
## 150          5.9         3.0          5.1         1.8  virginica
plot <- plot_ly (x = iris$Species, y = colnames(iris_matrix), z = iris_matrix, type = "heatmap") %>%
  layout(margin = list(l=120))
plot
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
<<<<<<< HEAD
=======
>>>>>>> 595fff5556ffc5041ce8006955a6476aee16dd06 >>>>>>> cb3f12a9bb56803e7f87a128236da8efc0477c80 >>>>>>> b623b5b9a05678bddd9946a9764ade851a3847d7

3D Graphs

Plotly can also be used to create 3D versions of graphs, such as 3D scatterplots. In the following example from the Clustering Lab, a 3D plotly scatterplot is shown that graphs NBA players based on minutes played, field goals, and points. Hover over each point to see the player name and their salary.

#importing dataset and viewing data
nba = read_csv("/cloud/project/nba2020-21.csv")

#a function for pre-processing of the nba data
nba_pre_processing <- function(nba){
  stats <- nba[, c("Player","Tm", "Pos", "FG", "FT", "PTS", "MP")] %>%
mutate(Player = as.factor(Player)) %>%
  mutate(Pos=as.factor(Pos)) %>%
  mutate(Tm=as.factor(Tm)) %>%
  distinct(Player, .keep_all = TRUE)    #getting rid of duplicate names
  x <- gsub("[^[:alnum:]]", " ", stats$Player)  #getting rid of special chars
  mutate(stats, Player = x)
}

#calling the function
nba_stats <- nba_pre_processing(nba)
view(nba_stats)

#pre-processing for salary data
salary = read_csv("/cloud/project/nba_salaries_21.csv") 

#function for pre-processing salary data
salary_pre_processing <- function(salary){
  salary %>%
  mutate(Player = as.factor(Player)) %>%
  distinct(Player, .keep_all = TRUE)    #getting rid of duplicate names
  x <- gsub("[^[:alnum:]]", " ", salary$Player)  #getting rid of special chars
  mutate(salary, Player = x)
}

#calling salary function
salary_stats <- salary_pre_processing(salary) #processing salary data
view(salary_stats)
#combining nba data with salary data into one chart
nba_combined <- merge(nba_stats, salary_stats, by="Player", all=TRUE)  

#fixing column names 
colnames(nba_combined) <- c("Player", "Tm","Pos", "FG", "FT","PTS", "MP", "Salary")
#Creating a 3D graph

#creating 3D scatterplot using plotly
fig <- plot_ly(nba_combined,type = "scatter3d",mode = "markers", x = ~MP, y = ~FG, z = ~PTS, 
                text = ~paste('Player:', Player,'Salary:', Salary)) #to show player name and salary when hovering over points
 fig <- fig %>% layout(title = 'NBA Player Rankings Based on Minutes Played, Field Goals, and Points') #adding title
 fig <- fig %>% layout(scene = list(xaxis = list(title = 'Minutes Played'),
                                    yaxis = list(title = 'Field Goals'),
                                    zaxis = list(title = 'Points')))
<<<<<<< HEAD
#formatting axes
 
fig
======= fig
=======
#importing dataset and viewing data
nba = read_csv("/cloud/project/nba2020-21.csv")

#a function for pre-processing of the nba data
nba_pre_processing <- function(nba){
  stats <- nba[, c("Player","Tm", "Pos", "FG", "FT", "PTS", "MP")] %>%
mutate(Player = as.factor(Player)) %>%
  mutate(Pos=as.factor(Pos)) %>%
  mutate(Tm=as.factor(Tm)) %>%
  distinct(Player, .keep_all = TRUE)    #getting rid of duplicate names
  x <- gsub("[^[:alnum:]]", " ", stats$Player)  #getting rid of special chars
  mutate(stats, Player = x)
}

#calling the function
nba_stats <- nba_pre_processing(nba)
view(nba_stats)

#pre-processing for salary data
salary = read_csv("/cloud/project/nba_salaries_21.csv") 

#function for pre-processing salary data
salary_pre_processing <- function(salary){
  salary %>%
  mutate(Player = as.factor(Player)) %>%
  distinct(Player, .keep_all = TRUE)    #getting rid of duplicate names
  x <- gsub("[^[:alnum:]]", " ", salary$Player)  #getting rid of special chars
  mutate(salary, Player = x)
}

#calling salary function
salary_stats <- salary_pre_processing(salary) #processing salary data
view(salary_stats)
#combining nba data with salary data into one chart
nba_combined <- merge(nba_stats, salary_stats, by="Player", all=TRUE)  

#fixing column names 
colnames(nba_combined) <- c("Player", "Tm","Pos", "FG", "FT","PTS", "MP", "Salary")
#Creating a 3D graph

#creating 3D scatterplot using plotly
fig <- plot_ly(nba_combined,type = "scatter3d",mode = "markers", x = ~MP, y = ~FG, z = ~PTS, 
                text = ~paste('Player:', Player,'Salary:', Salary)) #to show player name and salary when hovering over points
 fig <- fig %>% layout(title = 'NBA Player Rankings Based on Minutes Played, Field Goals, and Points') #adding title
 fig <- fig %>% layout(scene = list(xaxis = list(title = 'Minutes Played'),
                                    yaxis = list(title = 'Field Goals'),
                                    zaxis = list(title = 'Points')))
<<<<<<< HEAD
#formatting axes
 
fig
======= fig
>>>>>>> 595fff5556ffc5041ce8006955a6476aee16dd06 >>>>>>> cb3f12a9bb56803e7f87a128236da8efc0477c80 >>>>>>> b623b5b9a05678bddd9946a9764ade851a3847d7